package com.kins.so.chatassistant.service.impl;

import com.kins.so.chatassistant.mapper.ChatQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;

import com.alibaba.fastjson.JSONObject;
import com.kins.so.chatassistant.entity.ChatQuestion;
import com.kins.so.chatassistant.entity.ChatQuestionMongodb;
import com.kins.so.chatassistant.entity.ChatSessions;
import com.kins.so.chatassistant.service.IChatAssistantService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: 申科
 * @Date:2025/11/26 17:17
 */
@Service
public class ChatQuestionServiceImpl extends ServiceImpl<ChatQuestionMapper, ChatQuestion> implements ChatQuestionService{
    @Autowired
    private ChatAssistantMapper chatAssistantMapper;
    @Autowired
    private IKnowledgeBaseService knowledgeBaseService;

    @Autowired
    private IChatAssistantService chatAssistantService;

    @Autowired
    CommonUtils commonUtils;

    @Override
    public ChatSessions getChatSessions(){
        SysUser sysUser = commonUtils.getCurrLoginUser();
        ChatAssistant chatAssistant = new ChatAssistant();
        chatAssistant.setId(sysUser.getUserNum());
        chatAssistant = chatAssistantMapper.getInfoById(chatAssistant);
        chatAssistantService.deleteChatAssistant(chatAssistant);

        chatAssistant = new ChatAssistant();
        List<RagflowLibrary> libraryList = knowledgeBaseService.findListByPermissionsByParam(new RagflowLibrary());
        List<String> libraryIds = knowledgeBaseService.parsedLibraryIds();
        String libIds = libraryList.stream().filter(item->libraryIds.indexOf(item.getLibraryId())!=-1).map(item->item.getLibraryId()).collect(Collectors.joining(","));
        chatAssistant.setAssistantName("信贷智能助手-"+sysUser.getUserName());
        chatAssistant.setLibraryId(libIds);
        chatAssistant.setId(sysUser.getUserNum());
        chatAssistant = chatAssistantService.saveLibrary(chatAssistant);

        List<Object> chatSessionList = chatAssistantService.getChatSessionList(chatAssistant.getId());
        ChatSessions chatSessions = new ChatSessions();

        if(CollectionUtils.isNotEmpty(chatSessionList)){
            chatSessions.setChatId(chatAssistant.getId());
            chatSessions.setSessionName("默认会话");
            chatSessions = chatAssistantService.saveOrUpdataSessions(chatSessions);
        }else{
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(chatSessionList.get(0)));

            chatSessions.setSessionName("默认会话");
            chatSession.setSessionId(jsonObject.getString("id"));
        }
        chatSessions.setChatId(chatAssistant.getAssistantId());
        return chatSessions;
    }

    @Autowired
    private ChatQuestionMongodbRepository chatQuestionMongodbRepository;

    @Override
    public boolean save(ChatQuestion chatQuestion) {
        SysUser sysUser = commonUtils.getCurrLoginUser();
        chatQuestion.setCreateBy(sysUser.getUserNum());
        chatQuestion.setCreateDate(new Date());
        chatQuestion.setUpdateBy(sysUser.getUserNum());
        chatQuestion.setUpdateDate(new Date());
        chatQuestion.setTenantId(sysUser.getTenantId());
        ChatQuestionMongodb chatQuestionMongodb = new ChatQuestionMongodb();
        chatQuestion.setId(chatQuestion.getId())
        chatQuestionMongodb.setId(chatQuestion.getId());
        chatQuestionMongodb.setQuestionReply(chatQuestion.getQuestionReply());
        chatQuestionMongodb.setQuestion(chatQuestion.getQuestion());
        chatQuestionMongodbRepository.save(chatQuestionMongodb);
        return super.save(chatQuestion);
    }
}
